/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cuong.bean;

import client.entity.Category;
import client.entity.Customer;
import client.entity.JobSubject;
import client.entity.LevelJob;
import client.entity.Location;
import client.entity.Recruitment;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author Admin Windows 8
 */
@ManagedBean
@SessionScoped
public class TopTenJobBean implements Serializable{

    /**
     * Creates a new instance of TopTenJobBean
     */
    public TopTenJobBean() {       
       
    }
  
    private List<JobSubject> list;
    private List<Recruitment> listRecruiment;
  

    public List<JobSubject> getList() {
         list = new ArrayList<JobSubject>();       
         List<Recruitment> listRe = getListRecruiment();
         for (Recruitment recruitment : listRe) {
            JobSubject js= new JobSubject();
            js.setId(recruitment.getId());
            js.setTitle(recruitment.getTitle());
            if(recruitment.getTypeOfJob()==true){
                js.setTypejob("Full Time");
            }else{
                js.setTypejob("Pass Time");
            }
            js.setNamecustomer(getCustomerByID(recruitment.getCustomer().getId()).getName());
            js.setNameleveljob(getLevelJobByID(recruitment.getLevelJob().getId()).getName());
            
            List<Category> listCate= getListCateByIDRecrui(recruitment.getId());
            StringBuffer stringCate= new StringBuffer();
            for(int i=0;i<listCate.size();i++){
                Category ct=listCate.get(i);
                if(i<listCate.size()-1){
                    stringCate.append(ct.getCategoryName()+", ");
                }else{
                    stringCate.append(ct.getCategoryName());
                }
            }
            js.setNamecate(stringCate.toString());
            
            List<Location> listLoca=getListLocaByIDRecrui(recruitment.getId());            
            StringBuffer stringLocale= new StringBuffer();
             for(int i=0;i<listLoca.size();i++){
                Location lc=listLoca.get(i);
                if(i<listLoca.size()-1){
                    stringLocale.append(lc.getName()+", ");
                }else{
                    stringLocale.append(lc.getName());
                }
            }
             js.setNamelocation(stringLocale.toString());
            list.add(js);
        }
        return list;
    }

    public void setList(List<JobSubject> list) {
        this.list = list;
    }

    public List<Recruitment> getListRecruiment() {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
       listRecruiment=ss.createQuery("from Recruitment re order by re.id desc").setMaxResults(10).list();
       ss.getTransaction().commit();
        ss.close();
        return listRecruiment;
    }
    public List<Category> getListCateByIDRecrui(int idRecrui){
        List<Category> list = new ArrayList<Category>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        list = ss.createQuery("select c from Category as c inner join c.recruitmentCategories as rc where rc.recruitment.id="+idRecrui).list();       
        ss.getTransaction().commit();
        ss.close();
        return list;
    }
    public List<Location> getListLocaByIDRecrui(int idRecrui){
        List<Location> list = new ArrayList<Location>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        list = ss.createQuery("select lo from Location as lo inner join lo.recruitmentLocations as rl where rl.recruitment.id="+idRecrui).list();       
        ss.getTransaction().commit();
        ss.close();
        return list;
    }
    public LevelJob getLevelJobByID(int id){
        LevelJob lj = new LevelJob();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        lj = (LevelJob) ss.createQuery("from LevelJob lj where lj.id="+id).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return lj;
    }
    public Customer getCustomerByID(int id){
        Customer lj = new Customer();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        lj = (Customer) ss.createQuery("from Customer cus where cus.id="+id).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return lj;
    }
    

    public void setListRecruiment(List<Recruitment> listRecruiment) {
        this.listRecruiment = listRecruiment;
    }

  
    
}
